package com.djx.db.service;

import com.djx.comm.Pagination;
import com.djx.comm.entity.Account;
import com.djx.comm.entity.User;
import com.djx.db.mapper.db1.MasterUserMapper;
import com.djx.db.mapper.db2.SlaveUserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户接口实现
 * @author dingjunxiong
 */
@Service
public class UserService {
    private static final Logger logger = LoggerFactory.getLogger(UserService.class);

    @Resource
    MasterUserMapper masterUserMapper;

    @Resource
    SlaveUserMapper slaveUserMapper;

    public User queryById1(String id) throws Exception {
        User user = masterUserMapper.queryById(id);
        if (null != user) {
            user.aesDecryptVo();
        }
        return user;
    }
    public Pagination queryPage1(User user) {
        Pagination pagination = new Pagination();
        if (null != user.getPageNum() && user.getPageNum() > 0) {
            pagination.setPageNum(user.getPageNum());
        }
        if (null != user.getPageSize() && user.getPageSize() > 0) {
            pagination.setPageSize(user.getPageSize());
        }
        pagination.startPage();
        List<User> data = masterUserMapper.queryList(user);
        pagination.setResult(data);
        if (null != data) {
            data.stream().forEach(x -> {
                try {
                    x.aesDecryptVo();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
        return pagination;
    }

    public Account queryById2(String id) throws Exception {
        Account account = slaveUserMapper.queryById(id);
        if (null != account) {
            account.aesDecryptVo();
        }
        return account;
    }
    public Pagination queryPage2(Account account) {
        Pagination pagination = new Pagination();
        if (null != account.getPageNum() && account.getPageNum() > 0) {
            pagination.setPageNum(account.getPageNum());
        }
        if (null != account.getPageSize() && account.getPageSize() > 0) {
            pagination.setPageSize(account.getPageSize());
        }
        pagination.startPage();
        List<Account> data = slaveUserMapper.queryList(account);
        pagination.setResult(data);
        if (null != data) {
            data.stream().forEach(x -> {
                try {
                    x.aesDecryptVo();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
        return pagination;
    }

}
